package cn.tedu.hotel.serve.service.impl;

import cn.tedu.hotel.serve.ex.ServiceException;
import cn.tedu.hotel.serve.mapper.HotelComplainingMapper;
import cn.tedu.hotel.serve.mapper.HotelMapper;
import cn.tedu.hotel.serve.mapper.UserMapper;
import cn.tedu.hotel.serve.pojo.dto.HotelComplainingDTO;
import cn.tedu.hotel.serve.pojo.entity.HotelComplaining;
import cn.tedu.hotel.serve.pojo.vo.HotelComplainingVO;
import cn.tedu.hotel.serve.pojo.vo.HotelStandardVO;
import cn.tedu.hotel.serve.pojo.vo.UserStandardVO;
import cn.tedu.hotel.serve.service.IHotelComplainingService;
import cn.tedu.hotel.serve.service.IHotelService;
import cn.tedu.hotel.serve.service.IUserService;
import cn.tedu.hotel.serve.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class HotelComplainingServiceImpl implements IHotelComplainingService {

    @Autowired
    HotelComplainingMapper hotelComplainingMapper;

    @Autowired
    HotelMapper hotelMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public void addHotelComplain(HotelComplainingDTO hotelComplainingDTO) {
        log.debug("开始处理【前台用户对酒店进行投诉】的业务，参数：{}",hotelComplainingDTO);
        HotelStandardVO hotelStandardVO =
                hotelMapper.getHotelById(hotelComplainingDTO.getHotelId());
        if (hotelStandardVO==null){
            String message = "酒店不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        UserStandardVO userStandardVO =
                userMapper.getStandardById(hotelComplainingDTO.getUserId());
        if (userStandardVO==null){
            String message = "用户不存在，请先登录！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        HotelComplaining hotelComplaining=new HotelComplaining();
        BeanUtils.copyProperties(hotelComplainingDTO,hotelComplaining);
        hotelComplaining.setState(HOTEL_COMPLAINING_UNDER_REVIEW);
        hotelComplaining.setCreated(LocalDateTime.now());
        int rows = hotelComplainingMapper.addHotelComplain(hotelComplaining);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public List<HotelComplainingVO> getAdminHotelComplaining() {
        log.debug("开始处理【后台查询投诉列表】的业务");
        List<HotelComplainingVO> list = hotelComplainingMapper.getAdminHotelComplaining();
        log.debug("开始处理【后台查询投诉列表】的业务,数据：{}",list);
        return list;
    }

    @Override
    public void updateHotelComplainingById(Long id,String feedbackContent) {
        log.debug("开始处理【后台根据投诉ID：{}修改投诉状态并且反馈内容：{}】的业务",id,feedbackContent);
        if (feedbackContent==null){
            String message = "反馈内容不能为空！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST, message);
        }
        HotelComplainingVO hotelComplainingVO =
                hotelComplainingMapper.getAdminHotelComplainingById(id);
        if (hotelComplainingVO==null){
            String message = "投诉信息不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = hotelComplainingMapper
                .updateHotelComplainingById(id,HOTEL_COMPLAINING_APPROVED,feedbackContent);
        if (rows!=1){
            String message = "投诉信息不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
    }

    @Override
    public List<HotelComplainingVO> getHotelComplainingListById(Long userId) {
        log.debug("开始处理【前台根据用户id：{}查询用户的投诉信息】的业务",userId);
        UserStandardVO standardVO = userMapper.getStandardById(userId);
        if (standardVO==null){
            String message = "用户不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<HotelComplainingVO> list =
                hotelComplainingMapper.getHotelComplainingListById(userId);
        log.debug("开始处理【前台根据用户id：{}查询用户的投诉信息】的业务,数据：{}",userId,list);
        return list;
    }

    @Override
    public void deleteAdminHotelComplainingById(Long id) {
        log.debug("开始处理【后台根据投诉id：{}删除用户的投诉信息】的业务",id);
        HotelComplainingVO hotelComplainingVO =
                hotelComplainingMapper.getAdminHotelComplainingById(id);
        if (hotelComplainingVO==null){
            String message = "投诉内容不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = hotelComplainingMapper.deleteAdminHotelComplainingById(id);
        if (rows!=1){
            String message = "投诉内容不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }
}
